package com.matchmaking.system.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.db.dialect.impl.H2Dialect;
import cn.hutool.db.dialect.impl.MysqlDialect;
import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.extra.tokenizer.engine.ikanalyzer.IKAnalyzerEngine;
import com.alibaba.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.matchmaking.system.config.PageBean;
import com.matchmaking.system.config.R;
import com.matchmaking.system.domain.Bing;
import com.matchmaking.system.domain.SysRole;
import com.matchmaking.system.domain.SysRoleMenu;
import com.matchmaking.system.domain.ZhuZhuang;
import com.matchmaking.system.domain.vo.SysMenu;
import com.matchmaking.system.service.SysMenuService;
import com.matchmaking.system.service.SysRoleService;
import com.sun.corba.se.spi.orbutil.threadpool.WorkQueue;
import net.sf.jsqlparser.statement.select.IntoTableVisitor;
import org.bouncycastle.asn1.ntt.NTTObjectIdentifiers;
import org.bouncycastle.crypto.generators.KDF1BytesGenerator;
import org.bouncycastle.crypto.generators.X448KeyPairGenerator;
import org.bouncycastle.jcajce.provider.asymmetric.edec.KeyFactorySpi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.test.web.reactive.server.KotlinBodySpec;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.Yaml;

import javax.swing.*;
import javax.swing.text.html.CSS;
import java.io.OutputStream;
import java.time.Year;
import java.util.*;
import java.util.logging.MemoryHandler;

@RestController
@RequestMapping("role")
public class SysRoleController {
    @Autowired
    SysMenuService sysMenuService;

    @Autowired
    SysRoleService sysRoleService;
    /**
     * 批删
     * @return
     */
    @PostMapping("delall")
    public R delall(String roleId){
        String[] split = roleId.split(",");
        for (String s : split) {
            sysRoleService.deleteObjectById(s);
        }
        return R.successed(null);
    }

    /**
     *数据范围报表
     * dataScopeTable
     * @return
     */
    @PostMapping("dataScopeTable")
    public R dataScopeTable(){
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> x = new ArrayList<>();
        ArrayList<String> y = new ArrayList<>();
        List<ZhuZhuang>  list =  sysRoleService.scopeTable();
        for (ZhuZhuang zhuZhuang : list) {
            x.add(zhuZhuang.getX());
            y.add(zhuZhuang.getY());
        }
        map.put("x",x);
        map.put("y",y);
      return R.successed(map);
    }

    /*
    状态饼图报表
     */
    @PostMapping ("statusTable")
     public R  statusTable(){

        List<Bing> rolelist  =  sysRoleService.statusTable();
        return R.successed(rolelist);
    }

    /*
    给角色赋权限
     */
    @PostMapping("jurisData")
    public R jurisData(@RequestBody SysRole sysRole){
        if (sysRole.getMenuId()==null){
            sysRoleService.updateObject(sysRole);
        }else{
            SysMenu[] menuId = sysRole.getMenuId();
            for (SysMenu sysMenu : menuId) {
                String value = sysMenu.getValue();
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(sysRole.getRoleId());
                sysRoleMenu.setMenuId(value);
                System.err.println(sysRoleMenu);
                sysRoleService.saveRoleMenu(sysRoleMenu);
            }
        }


         return R.successed(null);
    }


    /*
    树形菜单
     */
    @PostMapping("getParentMenu")
    public R getparentMenu(){
        List<com.matchmaking.system.domain.SysMenu> list = sysMenuService.getParentMenu();

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 自定义属性名 都要默认值的
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setIdKey("value");
// 最大递归深度
        treeNodeConfig.setDeep(3);

//转换器
        List<Tree<String>> treeNodes = TreeUtil.build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getMenuId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                     tree.setName(treeNode.getMenuName());
                });
   return R.successed(treeNodes);

    }

    /*
    开关修改状态
     */
    @PostMapping("roleUpdStatus")
    public R updstatus(@RequestBody SysRole sysRole){
        sysRoleService.updateStatus(sysRole);
        if ("0".equals(sysRole.getStatus())){
            return R.successed("状态已改为正常");
        }else if ("1".equals(sysRole.getStatus())){
            return R.successed("状态已改为停用");
        }
       return R.successed(sysRole.getStatus());
    }

     /*
     添加修改
      */
    @PostMapping("addAndUpd")
    public R addandupd(@RequestBody SysRole sysRole){
        if (sysRole.getRoleId()==null){
            sysRole.setCreateTime(new Date());
            boolean r =  sysRoleService.insertObject(sysRole);
            return R.successed("添加成功");
        }else{
          boolean  k =  sysRoleService.updateObject(sysRole);
            return R.successed("修改成功");
        }


    }
     /*
     查询所有列表
      */
     @RequestMapping("listRole")
     public R sysRoleList(){
         return R.successed(sysRoleService.selectAll());
     }
     /*
     根据id删除
      */
    @DeleteMapping("delRoleById")
    public R sysRoleByid(String roleId){
     int rolenum =  sysRoleService.deleteObjectById(roleId);
     return R.successed("删除成功");
    }
    /*
    根据条件查询列表
     */
    @GetMapping("listRoleObjects")
    public R listRoleObjects(SysRole role,PageBean pageBean){
        System.err.println(role+""+pageBean);
        PageHelper.startPage(pageBean);
         List<SysRole> rolelist =   sysRoleService.selectObject(role);
        PageInfo<SysRole> sysRolePageInfo = new PageInfo<>(rolelist);
        return R.successed(sysRolePageInfo);
    }

}
